﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DbCommon;
using System.Data;
using System.Text;
using Entity;
using BLL;


namespace Web.frontpage
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        AcademyService academyService = new AcademyService();
        StudentService studentService = new StudentService();
        ContestService contestService = new ContestService();

        static DataTable table;
        static int ItemCount;//总条数
        static int PageCount;//总页数
        static int PageIndex;//当前页
        static int PageSize = 15;//分页数据长度
        public static string title;
        string ItemId;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            AcademyList.DataSource = academyService.getAllAcademies();
            AcademyList.DataTextField = "ACA_NAME";
            AcademyList.DataValueField = "ACA_ID";
            AcademyList.DataBind();
            this.AcademyList.Items.Add("全部");

            ContestList.DataSource = contestService.getAllContests();
            ContestList.DataTextField = "CON_NAME";
            ContestList.DataValueField = "CON_ID";
            ContestList.DataBind();
            this.ContestList.Items.Add("全部");
            
            if (!IsPostBack)
            {
                ItemId = Request.QueryString["ItemId"];
                //每次第一次加载的时候就将当前页置为1
                PageIndex = 1;
                RequestCheck();
                //总页数
                PageCount = (ItemCount + PageSize - 1) / PageSize;
                for (int i = 1; i <= PageCount; i++)
                {
                    DDLPager.Items.Add(new ListItem("第" + i.ToString() + "页", i.ToString()));
                }
                DataBind();
            }
        }
        protected void ButtonOK_Click(object sender, EventArgs e)
        {
            BoStudents stu = new BoStudents();
            
        }
        protected void RequestCheck()
        {
            ItemCount = (int)SQLHelper.GetRecordCount("","");
            
        }
        public void GetTable()
        {
            int StartItem;//开始条数
            int EndItem;//结束条数
            //开始页码所要获得的条数
            StartItem = PageSize * (PageIndex - 1);
            //条件选择是否超过总的条数
            EndItem = ItemCount > StartItem + PageSize ? StartItem + PageSize : ItemCount;
            BLL.NewsContent news = new BLL.NewsContent();
            BLL.ContestService contest = new BLL.ContestService();
            switch (Request.QueryString["ClassId"])
            {
                case "0": //校内公告
                    table = news.GetPage(StartItem.ToString(), EndItem.ToString(), " NewspubTime desc", "NewsClassId = '15'");
                    break;
                case "1": //竞赛通知
                    table = contest.GetPage(StartItem.ToString(), EndItem.ToString(), "CON_PUBTIME desc", "CON_ISCHECK=1");
                    break;
                case "2": //校内竞赛通知
                    table = contest.GetPage(StartItem.ToString(), EndItem.ToString(), "CON_PUBTIME desc", "COM_TYPE=0 ");
                    break;
                case "3": //省级竞赛
                    table = contest.GetPage(StartItem.ToString(), EndItem.ToString(), "CON_STARTTIME desc", "COM_TYPE=1 ");
                    break;
                case "4": //管理制度
                    table = news.GetPage(StartItem.ToString(), EndItem.ToString(), " NewspubTime desc", "NewsClassId = '2'");
                    break;
                case "5": //下载中心
                    table = news.GetPage(StartItem.ToString(), EndItem.ToString(), " NewspubTime desc", "NewsClassId = '3'");
                    break;
                //case "6": //作品展示
                //    ItemCount = (int)SQLHelper.GetRecordCount("NewsContent", "NewsClassId = 15");
                //    break;
                case "7"://竞赛项目
                    if (!string.IsNullOrEmpty(ItemId))
                    {
                        table = contest.GetPage(StartItem.ToString(), EndItem.ToString(), "CON_STARTTIME desc", "CON_COMPETITIONS='" + ItemId + "'");
                        title = table.Rows[0]["COM_NAME"].ToString();
                    }
                    else
                        Response.Write("<script>alert('禁止修改地址')</script>;window.location.href='index.aspx'");
                    break;
            }
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        protected void DataBind()
        {
            //两个if语句用来处理下一页或者上一页的刷新效果
            if (PageIndex > PageCount)
                PageIndex = PageCount;
            else if (PageIndex < 1)
                PageIndex = 1;
            GetTable();
            //绑定当前的前台
            //当前总的页数
            lbPageAll.InnerText = ItemCount.ToString();
            lbPager.InnerText = PageCount.ToString();
            lbIndexPage.InnerText = PageIndex.ToString();
            DDLPager.SelectedValue = PageIndex.ToString();
            if (PageIndex <= 1)
            {
                First.Enabled = false;
                Prev.Enabled = false;
            }
            else
            {
                First.Enabled = true;
                Prev.Enabled = true;
            }
            if (PageIndex >= PageCount)
            {
                Next.Enabled = false;
                Last.Enabled = false;
            }
            else
            {
                Next.Enabled = true;
                Last.Enabled = true;
            }
        }
        public string GetList()
        {
            StringBuilder sbd = new StringBuilder();


            switch (Request.QueryString["ClassId"])
            {
                case "0": //新闻公告
                    foreach (DataRow dr in table.Rows)
                    {
                        sbd.Append("<tr>");
                        sbd.Append("<td width=\"75%\" height=\"18\">");
                        sbd.Append("<a href=\"Third.aspx?ClassId=0&Id=" + dr["NewsId"] + "\">" + dr["NewsTitle"] + "</a>");
                        sbd.Append("</td><td width=\"20%\" align=\"right\">");
                        sbd.Append(((DateTime)dr["NewspubTime"]).ToString("yyyy-MM-dd") + "</td></tr>");
                    }
                    break;
                case "1": //竞赛通知
                    foreach (DataRow dr in table.Rows)
                    {
                        sbd.Append("<tr>");
                        sbd.Append("<td width=\"75%\" height=\"18\">");
                        sbd.Append("<a href=\"Third.aspx?ClassId=2&Id=" + dr["CON_ID"] + "\">" + dr["CON_NAME"] + "</a>");
                        sbd.Append("</td><td width=\"20%\" align=\"right\">");
                        sbd.Append(((DateTime)dr["CON_PUBTIME"]).ToString("yyyy-MM-dd") + "</td></tr>");
                    }
                    break;
                case "2": //校内竞赛
                    foreach (DataRow dr in table.Rows)
                    {
                        sbd.Append("<tr>");
                        sbd.Append("<td width=\"75%\" height=\"18\">");
                        sbd.Append("<a href=\"Third.aspx?ClassId=2&Id=" + dr["CON_ID"] + "\">" + dr["CON_NAME"] + "</a>");
                        sbd.Append("</td><td width=\"20%\" align=\"right\">");
                        sbd.Append(((DateTime)dr["CON_PUBTIME"]).ToString("yyyy-MM-dd") + "</td></tr>");
                    }
                    break;
                case "3": //省级竞赛
                    foreach (DataRow dr in table.Rows)
                    {
                        sbd.Append("<tr>");
                        sbd.Append("<td width=\"75%\" height=\"18\">");
                        sbd.Append("<a href=\"Third.aspx?ClassId=2&Id=" + dr["CON_ID"] + "\">" + dr["CON_NAME"] + "</a>");
                        sbd.Append("</td><td width=\"20%\" align=\"right\">");
                        sbd.Append(((DateTime)dr["CON_PUBTIME"]).ToString("yyyy-MM-dd") + "</td></tr>");
                    }
                    break;
                case "4": //管理制度
                    foreach (DataRow dr in table.Rows)
                    {
                        sbd.Append("<tr>");
                        sbd.Append("<td width=\"75%\" height=\"18\">");
                        sbd.Append("<a href=\"Third.aspx?ClassId=0&Id=" + dr["NewsId"] + "\">" + dr["NewsTitle"] + "</a>");
                        sbd.Append("</td><td width=\"20%\" align=\"right\">");
                        sbd.Append(((DateTime)dr["NewspubTime"]).ToString("yyyy-MM-dd") + "</td></tr>");
                    }
                    break;
                case "5": //下载中心
                    foreach (DataRow dr in table.Rows)
                    {
                        sbd.Append("<tr>");
                        sbd.Append("<td width=\"75%\" height=\"18\">");
                        sbd.Append("<a href=\"Third.aspx?ClassId=0&Id=" + dr["NewsId"] + "\">" + dr["NewsTitle"] + "</a>");
                        sbd.Append("</td><td width=\"20%\" align=\"right\">");
                        sbd.Append(((DateTime)dr["NewspubTime"]).ToString("yyyy-MM-dd") + "</td></tr>");
                    }
                    break;
                //case "6": //作品展示
                //    ItemCount = (int)SQLHelper.GetRecordCount("NewsContent", "NewsClassId = 15");
                //    break;
                case "7"://项目列表
                    foreach (DataRow dr in table.Rows)
                    {
                        sbd.Append("<tr>");
                        sbd.Append("<td width=\"75%\" height=\"18\">");
                        sbd.Append("<a href=\"Third.aspx?ClassId=2&Id=" + dr["CON_ID"] + "\">" + dr["CON_NAME"] + "</a>");
                        sbd.Append("</td><td width=\"20%\" align=\"right\">");
                        sbd.Append(((DateTime)dr["CON_PUBTIME"]).ToString("yyyy-MM-dd") + "</td></tr>");
                    }
                    break;
            }
            return sbd.ToString();
        }
        /// <summary>
        /// 页码点击事件
        /// </summary>
        /// <param name="sender">对象</param>
        /// <param name="e">事件</param>
        protected void PageClick(object sender, EventArgs e)
        {
            //当前页码
            int iCurrentPageIndex = PageIndex;
            //页码总数
            int iPageCount = PageCount;
            string sCommandName = (sender as LinkButton).CommandName;
            switch (sCommandName)
            {
                case "First":
                    PageIndex = 1;
                    break;
                case "Last":
                    PageIndex = iPageCount;
                    break;
                case "Prev":
                    PageIndex = iCurrentPageIndex - 1;
                    break;
                case "Next":
                    PageIndex = iCurrentPageIndex + 1;
                    break;
            }
            DataBind();

        }
        /// <summary>
        /// 页码下拉列表改变事件
        /// </summary>
        /// <param name="sneder">对象</param>
        /// <param name="e">事件</param>
        protected void DDLpager_Change(object sneder, EventArgs e)
        {
            PageIndex = Convert.ToInt32(DDLPager.SelectedValue);
            DataBind();
        }
    }
}